/**
 * Created by zhang on 2016/12/14.
 */
var accessManage = {
    events:{
        "#accessManageBody .modify-role click" : "modifyRole",
        "#accessManageBody .delete-role click" : "deleteUser",
        "#accessManageBody .reset-password click" : "resetPassword"
    },
    init: function () {
        var _this = this;
        document.title = "权限管理 - 系统管理";
        var temp = Util.template($("#accessManage-template").html(),{});
        $(".commonBody-rightBody").html(temp);
        Util.event.call(_this,_this.events);
        _this.initTable();
    },
    initTable: function (page) {
        var _this = this;
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.getUserList(),
            data:{
                currentPage:Util.isEmpty(page)?1:page,
                pageSize:pageInfo.pageSize()
            },
            showLoading : false,
            async:false,
            timeout: 100000,
            success : function(data){
                if(data.code == 200){
                    $.each(data.result.results, function (i, n) {
                        if (n.userName == "admin") {
                            n.caozuo = "<button class='btn btn-green reset-password' title='重置密码'>重置</button>";
                        }else{
                            n.caozuo = "<button class='btn btn-yellow mr10 modify-role'>修改</button>"+
                                    "<button class='btn btn-green mr10 reset-password' title='重置密码'>重置</button>"+
                                    "<button class='btn btn-red delete-role'>删除</button>";
                        }
                    });
                    $(".accessManage-list").cakeTable({
                        head: [   
                            {field: "realName",name: "姓名"},
                            {field: "depName",name: "部门"},
                            {field: "userName",name: "用户名"},
                            {field: "roleName",name: "用户权限"},
                            {field: "updateTime",name:"授权时间",format:"time"},
                            {field: "caozuo",name:"操作",type:2}
                        ],
                        rowData:[
                            {key:"id",attrName:"id"}
                        ],
                        columns:data.result.results,
                        sort:false,
                        showColumns :false,
                        pageInfo:data.result.pagination2,
                        align:"center",
                        pageType:"out",
                        page : function(page){
                            _this.initTable(page);
                        }
                    })
                }   
            }
        });
    },
    getRoles : function(){
        var result = null;
        Util.ajaxRequest({
            type: "get",
            url: interfaceURL.getRoles(),
            dataType: "json",
            async:false,
            success:function(data){
                if(data.code==200){
                    result = data.result;
                }
            }
        });
        return result;
    },
    userOperate : function(url,para,fn){
        Util.ajaxRequest({
            type: "get",
            url:url,
            dataType: "json",
            data:para,
            success : function(data){
                if(data.code == 200){
                    $.isFunction(fn)?fn():"";
                }
            }
        })
    },
    modifyRole : function(ele){
        var _this = this,
            data = {},
            roles = _this.getRoles(),
            $thisTr = $(ele).parents("tr"),
            id = $thisTr.attr("id");
            data["accessT"] = $thisTr.find("td:eq(4)").html();
            data["nameT"] = $thisTr.find("td:eq(1)").html();
            data["departT"] = $thisTr.find("td:eq(2)").html();
            data["userT"] = $thisTr.find("td:eq(3)").html();
        pop.dialog({
            popTitle: "用户修改",
            popContent: Util.template($("#modifyAccess-template").html(), {roles:roles,data:data}),
            width :　"500px"
        },function(){
            var roleId = $("#v-access").val(),
                url = interfaceURL.modifyRole();
            if (!Util.isEmpty(roleId)) {
                _this.userOperate(url,{
                    id:id,
                    roleId :roleId
                },function(){
                    pop.alert("修改成功！",function(){
                        _this.initTable();
                    })
                });
            }else{
                pop.alert("请选择角色！")
                return false
            }
        })
    },
    deleteUser : function(ele){
        var _this = this,
            url = interfaceURL.deleteUser(),
            id = $(ele).parents("tr").attr("id");
        pop.confirm("确定删除该用户吗？",function(){
            _this.userOperate(url,{id:id},function(){
                pop.alert("删除成功！",function(){
                   _this.initTable();
                })
            })
        })
    },
    resetPassword :function(ele){
        var _this = this,
            url = interfaceURL.resetPassword(),
            id = $(ele).parents("tr").attr("id");
        pop.confirm("确定重置密码吗？",function(){
            _this.userOperate(url,{id:id},function(){
                pop.alert("重置成功！")
            })
        })
    }
};